package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.UserWallet;
import com.sc.nft.entity.dto.ReportBalanceAndPayBalanceDTO;
import com.sc.nft.entity.dto.UpdateUserByIdBatchDTO;
import com.sc.nft.entity.vo.OperateUserWalletBatchListVO;
import com.sc.nft.entity.vo.UserWalletListVO;
import com.sc.nft.mapper.UserWalletMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户钱包表(sc_user_wallet)数据DAO
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-08-18 01:22:40
 */
@Slf4j
@Repository
public class UserWalletDao extends ServiceImpl<UserWalletMapper, UserWallet> {

    public UserWallet getByUserId(Long userId) {
        LambdaQueryWrapper<UserWallet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserWallet::getUserId, userId);
        return getOne(wrapper);
    }

    public Boolean addIncome(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("sum_income=sum_income+" + amount + ",balance=balance+" + amount);
        wrapper.eq(UserWallet::getUserId, userId);
        return update(new UserWallet(),wrapper);
    }

    public Boolean addWishBalance(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql( "wish_balance=wish_balance+" + amount);
        wrapper.eq(UserWallet::getUserId, userId);
        return update(new UserWallet(),wrapper);
    }

    public void recharge(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("balance=balance+" + amount);
        wrapper.eq(UserWallet::getUserId, userId);
        update(wrapper);
    }

    public void frozenAmount(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("balance=balance-" + amount + ",withdrawing_amount=withdrawing_amount+" + amount);
        wrapper.eq(UserWallet::getUserId, userId);
        update(wrapper);
    }

    public void withdrawSuccess(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("withdrawing_amount=withdrawing_amount-" + amount + ",sum_withdraw=sum_withdraw+" + amount);
        wrapper.eq(UserWallet::getUserId, userId);
        update(wrapper);
    }


    public void refound(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("withdrawing_amount=withdrawing_amount-" + amount + ",balance=balance+" + amount);
        wrapper.eq(UserWallet::getUserId, userId);
        update(wrapper);
    }



    public boolean subBalance(Long userId, BigDecimal productPrice) {
        return baseMapper.subBalance(userId, productPrice)>0;
    }

    public boolean subPayBalance(Long userId, BigDecimal productPrice) {
        return baseMapper.subPayBalance(userId, productPrice)>0;
    }
    public boolean subWishBalance(Long userId, BigDecimal productPrice) {
        return baseMapper.subWishBalance(userId, productPrice)>0;
    }

    public Boolean addBalance(Long userId, BigDecimal productPrice) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("balance=balance+" + productPrice);
        wrapper.eq(UserWallet::getUserId, userId);
        return update(wrapper);
    }

    public Page<UserWalletListVO> userWalletList(String userTel, int pageNo, int pageSize) {
        return baseMapper.userWalletList(userTel, new Page(pageNo, pageSize));
    }

    public Boolean addPayBalance(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("pay_balance=pay_balance+" + amount);
        wrapper.eq(UserWallet::getUserId, userId);
        return update(wrapper);
    }


    public Boolean transfer(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("balance=balance-" + amount+",transfer_amount=transfer_amount+ "+amount);
        wrapper.eq(UserWallet::getUserId, userId);
        return update(wrapper);
    }


    public Boolean transferSuccess(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("transfer_amount=transfer_amount-" + amount);
        wrapper.eq(UserWallet::getUserId, userId);
        return update(wrapper);
    }

    public Boolean transferFail(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("balance=balance+" + amount+",transfer_amount=transfer_amount-"+amount);
        wrapper.eq(UserWallet::getUserId, userId);
        return update(wrapper);
    }


    /**
     * 用户批量加钱减钱.
     *
     * @param updateUserByIdBatch
     * @param type                参考枚举
     *                            {@link com.sc.nft.enums.UserBalanceTypeEnum} type参考枚举
     * @return Boolean 成功or失败
     */
    public Boolean updateUserBatch(List<UpdateUserByIdBatchDTO> updateUserByIdBatch, Integer type) {
        Integer integer = baseMapper.updateUserBatch(updateUserByIdBatch, type);
        return integer>0;
    }

    public List<OperateUserWalletBatchListVO> operateUserWalletBatchList(List<String> userTels) {
        return baseMapper.operateUserWalletBatchList(userTels);
    }

    public ReportBalanceAndPayBalanceDTO getSumBalanceAndSumNowPayBalance() {
        return baseMapper.getSumBalanceAndSumNowPayBalance();
    }

    /**
     * 获取用户余额
     *
     * @param userId
     * @return
     */
    public BigDecimal getMallUserBalance(Long userId) {
        return baseMapper.getMallUserBalance(userId);
    }
}